package com.bootdo.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.shop.dao.CategoryDao;
import com.bootdo.shop.domain.CategoryDO;
import com.bootdo.shop.service.CategoryService;
import com.bootdo.system.domain.MenuDO;



@Service
public class CategoryServiceImpl implements CategoryService {
	@Autowired
	private CategoryDao categoryDao;
	
	@Override
	public CategoryDO get(Long categoryId){
		return categoryDao.get(categoryId);
	}
	
	@Override
	public List<CategoryDO> list(Map<String, Object> map){
		return categoryDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return categoryDao.count(map);
	}
	
	@Override
	public int save(CategoryDO category){
		return categoryDao.save(category);
	}
	
	@Override
	public int update(CategoryDO category){
		return categoryDao.update(category);
	}
	
	@Override
	public int remove(Long categoryId){
		return categoryDao.remove(categoryId);
	}
	
	@Override
	public int batchRemove(Long[] categoryIds){
		return categoryDao.batchRemove(categoryIds);
	}

	@Override
	public Tree<CategoryDO> getTree() {
		List<Tree<CategoryDO>> trees = new ArrayList<Tree<CategoryDO>>();
		List<CategoryDO> categorys = categoryDao.list(new HashMap<String,Object>(16));
		for (CategoryDO category : categorys) {
			Tree<CategoryDO> tree = new Tree<CategoryDO>();
			tree.setId(category.getCategoryId().toString());
			tree.setParentId(category.getParentId().toString());
			tree.setText(category.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<CategoryDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public List<Tree<CategoryDO>> listMenuTree() {
		List<Tree<CategoryDO>> trees = new ArrayList<Tree<CategoryDO>>();
		List<CategoryDO> categorys = categoryDao.list(new HashMap<String,Object>(16));
		for (CategoryDO category : categorys) {
			Tree<CategoryDO> tree = new Tree<CategoryDO>();
			tree.setId(category.getCategoryId().toString());
			tree.setParentId(category.getParentId().toString());
			tree.setText(category.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			/*attributes.put("url", sysMenuDO.getUrl());
			attributes.put("icon", sysMenuDO.getIcon());*/
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<CategoryDO>> list = BuildTree.buildList(trees, "0");
		return list;
	}

	@Override
	public List<CategoryDO> getCategoryBrotherList(Long categoryId) {
		
		return categoryDao.getCategoryBrotherList(categoryId);
	}
	
}
